const app = getApp()
var util = require('../../utils/time.js')
var util2 = require('../../utils/time2.js')
Page({
    data: {
        showDate: false,
        dateFormat: '',
        currentDate: '',
        currentTime: '',
        id: '',
        showTime: false,
        place: '',
        people: '',
        waterType: '',
        color: '',
        smell: '',
        muddy: '',
        oil: '',
        note: '',
        imageUrl: ''
    },
    onShow() {
        this.getTabBar().init();
    },
    onLoad: function () {
        // app.api = "http://127.0.0.1:5000/"
        app.api = 'https://api.magic-H.top/'
        this.setData({
            currentDate: (new Date).getTime(),
            dateFormat: this.formatDate(new Date()),
            currentTime: util2.formatTime(new Date()),
            showDate: false,
            id: '',
            showTime: false,
            place: '',
            people: '',
            waterType: '',
            color: '',
            smell: '',
            muddy: '',
            oil: '',
            note: ''
        })
    },
    onChangeID(event) {
        console.log(event)
        this.setData({
            id: event.detail
        })
    },
    onChangePlace(event) {
        this.setData({
            place: event.detail
        })
    },
    onChangePeople(event) {
        this.setData({
            people: event.detail
        })
    },
    onChangeWaterType(event) {
        this.setData({
            waterType: event.detail
        })
    },
    onChangeColor(event) {
        this.setData({
            color: event.detail
        })
    },
    onChangeSmell(event) {
        this.setData({
            smell: event.detail
        })
    },
    onChangeMuddy(event) {
        this.setData({
            muddy: event.detail
        })
    },
    onChangeOil(event) {
        this.setData({
            oil: event.detail
        })
    },
    onChangeNote(event) {
        this.setData({
            note: event.detail
        })
    },
    CloseDate() {
        this.setData({
            showDate: false
        });
    },
    DisplayDate() {
        this.setData({
            showDate: true
        });
    },
    formatDate(date) {
        return `${date.getFullYear()}/${(date.getMonth())+1}/${date.getDate()}`;
    },
    setDateValue(event) {
        this.setData({
            showDate: false,
            currentDate: event.detail,
            dateFormat: (new Date(event.detail)).toLocaleDateString()
        });
        this.CloseDate()
    },
    getID() {
        wx.scanCode({
            success: (res) => {
                this.setData({
                    id: res.result
                })
            }
        })
    },
    CloseTime(event) {
        this.setData({
            showTime: false
        })
    },
    DisplayTime() {
        this.setData({
            showTime: true
        })
    },
    setTimeValue(event) {
        this.setData({
            currentTime: event.detail
        })
        this.CloseTime()
    },
    submit() {
        let SumbitData = this.data
        let date = ''
        let time = ''
        for (var key in SumbitData) {
            if (key == 'dateFormat') {
                date = SumbitData[key]
            }
            if (key == 'currentTime') {
                time = SumbitData['currentTime']
            }
            // console.log(key,SumbitData[key])
        }
        SumbitData['date'] = date
        SumbitData['time'] = time
        // 去除SumbitData中的showDate、showTime、currentDate
        delete SumbitData['showDate']
        delete SumbitData['showTime']
        delete SumbitData['currentDate']
        delete SumbitData['__webviewId__']
        delete SumbitData['dateFormat']
        delete SumbitData['currentTime']
        console.log(SumbitData)
        //检查是否存在空值
        for (var i in SumbitData) {
            if (!SumbitData[i]) {
                wx.showToast({
                    title: '信息未填写完整',
                    icon: 'error',
                    duration: 2000
                })
                return;
            }
        }
        SumbitData['complete'] = false
        console.log("!!!", SumbitData)
        wx.request({
            // url: `http://127.0.0.1:5000/addSample`,
            url: app.api + "addSample",
            data: SumbitData,
            method: 'POST',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: (res) => {
                if (res.data === 'success') {
                    console.log(res)
                    wx.showToast({
                        title: '提交成功',
                        icon: 'success',
                        duration: 2000
                    })
                    this.setData({
                        currentDate: (new Date).getTime(),
                        dateFormat: this.formatDate(new Date()),
                        currentTime: util2.formatTime(new Date()),
                        showDate: false,
                        id: '',
                        showTime: false,
                        place: '',
                        waterType: '',
                        color: '',
                        smell: '',
                        muddy: '',
                        oil: '',
                        note: '',
                        imageUrl:''
                    })
                    // this.onLoad()
                } else {
                    console.log(res)
                    wx.showToast({
                        title: '添加样品失败',
                        icon: 'error',
                        duration: 2000
                    })
                }

            },
            fail(res) {
                console.log(res)
                wx.showToast({
                    title: '添加样品失败',
                    icon: 'error',
                    duration: 2000
                })
            }
        })
    },
    uploadImage() {
        let imageUrl = ''
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            maxDuration: 30,
            camera: 'back',
            success: res => {
                wx.showLoading({
                    title: '上传中',
                })
                console.log(res.tempFiles[0]['tempFilePath'])
                wx.uploadFile({
                    // url: 'http://127.0.0.1:5000/upload',
                    url: app.api + "upload",
                    filePath: res.tempFiles[0]['tempFilePath'],
                    name: 'file',
                    success: (res) => {
                        wx.hideLoading()
                        console.log(res)
                        this.setData({
                            imageUrl: res.data
                        })
                        //do something
                    },
                    fail: (res) => {
                        console.log(res)
                    }
                })


                // wx.cloud.uploadFile({
                //     cloudPath: 'img/' + (new Date()).getTime() + '.png',
                //     filePath: res.tempFiles[0]['tempFilePath'], // 文件路径
                //     success: res => {
                //         imageUrl = res.fileID
                //         // get resource ID
                //         this.setData({
                //             imageUrl: res.fileID
                //         })
                //         wx.hideLoading()
                //     },
                //     fail: err => {
                //         // handle error
                //         console.log(err)
                //     }
                // })

            }
        })

    }
});